﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using System.ComponentModel.DataAnnotations;
using IntroduceCompany.Util;
using IntroduceCompany.Resources;

namespace IntroduceCompany.Models
{
    public class FeedbackCreateModel
    {
        [Required(ErrorMessageResourceName = "Universal_ValidateString_Require", ErrorMessageResourceType = typeof(Resx))]
        [LocalizedDisplayName("FeedbackCreate_lab_FullName", NameResourceType = typeof(Resx))]
        public string FullName { get; set; }

        [LocalizedDisplayName("FeedbackCreate_lab_CompanyName", NameResourceType = typeof(Resx))]
        public string CompanyName { get; set; }

        [Required(ErrorMessageResourceName = "Universal_ValidateString_Require", ErrorMessageResourceType = typeof(Resx))]
        [RegularExpression(ConstantClass.testEmailRegular_1, ErrorMessageResourceName = "Universal_ValidateString_Email", ErrorMessageResourceType = typeof(Resx))]
        public string Email { get; set; }

        [RegularExpression(ConstantClass.testPhoneRegular_1, ErrorMessageResourceName = "Universal_ValidateString_Phone", ErrorMessageResourceType = typeof(Resx))]
        [LocalizedDisplayName("FeedbackCreate_lab_Phone", NameResourceType = typeof(Resx))]
        public string Phone { get; set; }

        public string Fax { get; set; }

        [Required(ErrorMessageResourceName = "Universal_ValidateString_Require", ErrorMessageResourceType = typeof(Resx))]
        [LocalizedDisplayName("FeedbackCreate_lab_Content", NameResourceType = typeof(Resx))]
        public string Content { get; set; }

        [Required(ErrorMessageResourceName = "Universal_ValidateString_Require", ErrorMessageResourceType = typeof(Resx))]
        [LocalizedDisplayName("FeedbackCreate_lab_Captcha", NameResourceType = typeof(Resx))]
        public string CaptchaValue { get; set; }
    }

    public class FeedbackModels
    {
        internal IQueryable<IntroduceCompany.Feedback> GetAllFeedback()
        {
            return DataSingleton.GetInstance().Feedbacks;
        }

        internal void ChangeFeebackIsAnswerStatus(int feedbackID)
        {
            Feedback feedback = DataSingleton.GetInstance().Feedbacks.SingleOrDefault(u => u.FeedbackID == feedbackID);
            feedback.IsAnswer = true;
            DataSingleton.GetInstance().Feedbacks.Context.SubmitChanges();
        }

        internal Feedback GetOneFeedback(int feedbackID)
        {
            return DataSingleton.GetInstance().Feedbacks.SingleOrDefault(u=>u.FeedbackID == feedbackID);
        }

        internal void CreateFeedback(FeedbackCreateModel obj_Feedback)
        {
            try
            {
                Feedback tempFeedback = new Feedback();

                tempFeedback.FullName = obj_Feedback.FullName.Trim();
                tempFeedback.CompanyName = obj_Feedback.CompanyName.Trim();
                tempFeedback.Email = obj_Feedback.Email.Trim();
                tempFeedback.Fax = obj_Feedback.Fax.Trim();

                tempFeedback.Phone = obj_Feedback.Phone.Trim();
                tempFeedback.Content = obj_Feedback.Content.Trim();
                tempFeedback.CreateDate = DateTime.Now;
                tempFeedback.IsAnswer = false;

                DataSingleton.GetInstance().Feedbacks.InsertOnSubmit(tempFeedback);
                DataSingleton.GetInstance().Feedbacks.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}